//
//  YAYIMessageCell.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/18.
//
//

#import "YAYIMessageCell.h"

#import "YAYIBadgeButton.h"
#import "YAYIMessageModel.h"
#import "NSDate+VR.h"

@interface YAYIMessageCell()

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel *titleLbl;
@property (nonatomic, weak) UILabel *desLbl;
@property (nonatomic, weak) UILabel *timeLbl;
@property (nonatomic, weak) UIView *topLineView;
@property (nonatomic, weak) UIView *bottomLineView;
@property (nonatomic, weak) YAYIBadgeButton *badgeButton;

@end


@implementation YAYIMessageCell

- (void)setLineType:(LineType)lineType
{
    _lineType = lineType;
    switch (lineType) {
        case LineTypeTop:
            self.topLineView.hidden = NO;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(15, 0);
            break;
        case LineTypeCenter:
            self.topLineView.hidden = YES;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(15, 0);
            break;
        case LineTypeBottom:
            self.topLineView.hidden = YES;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(0, 0);
            break;
        case LineTypeTopAndBottom:
            self.topLineView.hidden = NO;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(0, 0);
            break;
        default:
            break;
    }
}


- (void)setModel:(YAYIMessageModel *)model
{
    _model = model;
    
    if (model.content) {
        self.desLbl.text = model.content;
    }else{
        self.desLbl.text = @"雅同平台欢迎您";
    }
    
    
    self.badgeButton.badgeValue = [NSString stringWithFormat:@"%ld", model.news_count];//
    
    if (model.updatetime) {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.updatetime / 1000];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.locale = [NSLocale currentLocale];
        formatter.dateFormat = @"M月d日";
        
        if ([date isToday]) {
            formatter.dateFormat = @"ah:mm";
        }
        self.timeLbl.text = [formatter stringFromDate:date];
    }
}

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"messageCell";
    
    YAYIMessageCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupSubView];
    }
    return self;
}


- (void)setupSubView
{
    // 头像
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.image = [UIImage imageNamed:@"select"];
    iconView.layer.masksToBounds = YES;
    
    [self.contentView addSubview:iconView];
    self.iconView = iconView;
    
    // 标题label
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"雅同";
    titleLbl.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:titleLbl];
    self.titleLbl = titleLbl;
    
    // 简介label
    UILabel *desLbl = [[UILabel alloc] init];
    desLbl.text = @"雅同平台欢迎您";
    desLbl.font = [UIFont systemFontOfSize:16];
    desLbl.textColor = YAYIColor(154, 155, 156);
    [self.contentView addSubview:desLbl];
    self.desLbl = desLbl;
    
    // 时间label
//    NSDate *nowDate = [NSDate date];
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    formatter.dateFormat = @"ah:m";
//    NSString *nowDateStr = [formatter stringFromDate:nowDate];
    
    UILabel *timeLbl = [[UILabel alloc] init];
//    timeLbl.text = nowDateStr;
    timeLbl.font = [UIFont systemFontOfSize:13];
    timeLbl.textColor = YAYIColor(154, 155, 156);
    timeLbl.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:timeLbl];
    self.timeLbl = timeLbl;
    
    // 顶部线
    UIView *topLineView = [[UIView alloc] init];
    topLineView.backgroundColor = YAYICellLineColor;//YAYIColor(0xe0, 0xe0, 0xe0);
    [self.contentView addSubview:topLineView];
    self.topLineView = topLineView;
    topLineView.hidden = YES;
    
    // 底部线
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = YAYICellLineColor;
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
    
    //  添加badgeButton
    YAYIBadgeButton *badgeButton = [[YAYIBadgeButton alloc] init];
    
    badgeButton.badgeValue = @"77";
    [self.contentView addSubview:badgeButton];
    self.badgeButton = badgeButton;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(8);
        make.bottom.equalTo(self.contentView).offset(-8);
        make.width.equalTo(self.iconView.mas_height);
        make.left.equalTo(@(15));
    }];
    
    self.iconView.layer.cornerRadius = 18;
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView);
        make.left.equalTo(self.iconView.mas_right).offset(10);
    }];
    
    [self.timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLbl);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    [self.desLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView);
        make.left.equalTo(self.iconView.mas_right).offset(10);
        make.right.mas_equalTo(-50);
    }];
    
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.width.equalTo(@(SCREEN_width));
        make.top.equalTo(self.contentView).offset(-0);
        make.left.equalTo(self.contentView);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.width.equalTo(@(SCREEN_width));
        make.bottom.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView);
    }];
    
    [self.badgeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.desLbl).offset(0);
    }];
}

@end
